package com.example.pceb.mapper;

import com.example.pceb.entity.CooperationApplication;
import com.example.pceb.entity.SchoolEnterpriseCooperation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
import java.util.Map;

@Mapper
public interface CooperationApplicationMapper {
    @Insert("INSERT INTO cooperation_application (cooperation_id, applicant_name, application_content, proposal, resources_provided, application_status, review_remarks, review_time, publisher_username) " +
            "VALUES (#{cooperationId}, #{applicantName}, #{applicationContent}, #{proposal}, #{resourcesProvided}, #{applicationStatus}, #{reviewRemarks}, #{reviewTime}, #{publisherUsername})")
    int insert(CooperationApplication application);

    @Select("SELECT ca.* FROM cooperation_application ca " +
            "JOIN school_enterprise_cooperation sec ON ca.cooperation_id = sec.id " +
            "WHERE sec.publisher_username = #{publisherUsername} AND ca.application_status = '待审核'")
    List<CooperationApplication> selectByPublisherUsername(String publisherUsername);

    @Select("SELECT ca.* FROM cooperation_application ca " +
            "JOIN school_enterprise_cooperation sec ON ca.cooperation_id = sec.id " +
            "WHERE sec.publisher_type = #{publisherType} AND ca.application_status = '待审核'")
    List<CooperationApplication> selectByPublisherType(String publisherType);

    @Update("UPDATE cooperation_application SET " +
            "application_status = #{applicationStatus}, " +
            "review_remarks = #{reviewRemarks}, " +
            "review_time = #{reviewTime} " +
            "WHERE id = #{id}")
    int updateApplication(CooperationApplication application);
    
    @Select("SELECT ca.*, sec.publisher_type FROM cooperation_application ca " +
            "JOIN school_enterprise_cooperation sec ON ca.cooperation_id = sec.id " +
            "WHERE ca.id = #{id}")
    CooperationApplication selectByIdWithCooperationInfo(Long id);
    
    @Select("SELECT ca.*, sec.project_name, sec.project_type, sec.publisher_type FROM cooperation_application ca " +
            "JOIN school_enterprise_cooperation sec ON ca.cooperation_id = sec.id " +
            "WHERE ca.publisher_username = #{publisherUsername} " +
            "ORDER BY ca.id DESC")
    List<CooperationApplication> selectMyApplicationsByPublisherUsername(String publisherUsername);
    
    @Select("SELECT ca.*, sec.project_name, sec.project_type, sec.publisher_type FROM cooperation_application ca " +
            "JOIN school_enterprise_cooperation sec ON ca.cooperation_id = sec.id " +
            "WHERE ca.cooperation_id = #{cooperationId} " +
            "ORDER BY ca.id DESC")
    List<CooperationApplication> selectByCooperationId(Long cooperationId);

    @Select("SELECT ca.*, sec.project_name, sec.project_type, sec.project_description, sec.cooperation_content, sec.project_status, sec.school_name, sec.enterprise_name, sec.budget, sec.project_duration, sec.start_date, sec.end_date, sec.contact_person, sec.contact_phone, sec.contact_email, sec.remarks FROM cooperation_application ca JOIN school_enterprise_cooperation sec ON ca.cooperation_id = sec.id WHERE ca.publisher_username = #{publisherUsername} ORDER BY ca.id DESC")
    List<Map<String, Object>> selectMyApplicationsWithProjectByPublisherUsername(String publisherUsername);

    @Select("SELECT * FROM school_enterprise_cooperation WHERE id = #{id}")
    SchoolEnterpriseCooperation selectProjectById(Long id);
}